package com.ruoyi.waterManagement.controller;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.business.waterManagement.StationPicR;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.business.BusinessUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.datascope.annotation.DataScope;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.waterManagement.domain.vo.RzsWrStatvolHwrVO;
import com.ruoyi.waterManagement.domain.vo.WaterRegimeMonitoringVO;
import com.ruoyi.waterManagement.service.IWaterRegimeMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 水情在线监测站数据记录Controller
 *
 * @author hwd
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WaterRegimeMonitor")
public class WaterRegimeMonitoringController extends BaseController
{
    @Autowired
    private IWaterRegimeMonitoringService waterRegimeMonitoringService;

    /**
     * 查询水情监测实时数据记录列表
     */
    @RequiresPermissions("waterManagement:WaterRegimeMonitor:list")
    @DataScope(organAlias = "organb")
    @GetMapping("/list")
    public AjaxResult list(WaterRegimeMonitoringVO wt)
    {

        if (StringUtils.isEmpty(wt.getOrgCd())) {
            wt.setOrgCd(null);
        }
        if(wt.getOrgCd()!=null){
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wt.getOrgCd());
            wt.setOrgCd(addvAndOrgCodeInfo.get("resCode")+"");
        }
        List<WaterRegimeMonitoringVO> countList = waterRegimeMonitoringService.selWaterRegimeMonitoringList(new  WaterRegimeMonitoringVO());
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("installNum",countList.size());
        map.put("reportNum",countList.stream().filter(vo -> vo.getRunningState().equals("0")).count());//在线
        map.put("faultNum",countList.stream().filter(vo ->! "0".equals( vo.getRunningState())).count());//离线
        startPage();
        List<WaterRegimeMonitoringVO> list = waterRegimeMonitoringService.selWaterRegimeMonitoringList(wt);
        map.put("stationList",getDataTable(list));
        return AjaxResult.success(map);
    }

    @GetMapping("/queryStatNm")
    public  String queryStatNm (String statCd){
        return waterRegimeMonitoringService.queryStatNm(statCd);

    }

    /**
     * 查询水量监测实时数据记录列表
     */
    @RequiresPermissions("waterManagement:WaterRegimeMonitor:list")
    @DataScope(organAlias = "organb")
    @GetMapping("/listWaterYieldR")
    public AjaxResult wrStationRList(WaterRegimeMonitoringVO wt)
    {

        if (StringUtils.isEmpty(wt.getOrgCd())) {
            wt.setOrgCd(null);
        }
        if(wt.getOrgCd()!=null){
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wt.getOrgCd());
            wt.setOrgCd(addvAndOrgCodeInfo.get("resCode")+"");
        }
        List<WaterRegimeMonitoringVO> countList = waterRegimeMonitoringService.selWaterYieldRegimeMonitoringList(wt);
        startPage();
        List<WaterRegimeMonitoringVO> list = waterRegimeMonitoringService.selWaterYieldRegimeMonitoringList(wt);
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("installNum",countList.size());
        map.put("reportNum",countList.stream().filter(vo -> vo.getRunningState().equals("0")).count());//在线
        map.put("faultNum",countList.stream().filter(vo ->! "0".equals( vo.getRunningState())).count());//离线
        map.put("stationList",getDataTable(list));
        return AjaxResult.success(map);
    }


    /**
     * gIS查询渠道取水断面数据
     */
    @GetMapping("/queryCurrentChannelWaterIntake")
    public AjaxResult queryCurrentChannelWaterIntake(String  chanCd){

        if (chanCd==null){
            return AjaxResult.error("无渠道数据");
        }else {
            BigDecimal  currentChannelWater =waterRegimeMonitoringService.queryCurrentChannelWaterIntake(chanCd);
            HashMap<String, Object> chanCodeInfo = BusinessUtils.getChanCodeInfo(chanCd);
            int grade = (int) chanCodeInfo.get("grade");
            String resCode = (String) chanCodeInfo.get("resCode");
            if (grade == 1) {
                //查找下属水管机构
                resCode = resCode + "%000000";
            } else if (grade == 2) {
                //查找下属水管机构
                resCode = resCode + "%000";
            } else if (grade == 3) {
                resCode = resCode + "%";
            } else {
                resCode = resCode;
            }
         List<WaterRegimeMonitoringVO> chanBelowList=  waterRegimeMonitoringService.queryBelowChan(resCode,chanCd);
            //查询下属的渠道
            if (chanBelowList.size()>0){
                for (WaterRegimeMonitoringVO w:chanBelowList
                     ) {

                }
            }
        }
        return AjaxResult.success();
    }

    /**
     * 导出测站实时图片列表
     */
    @RequiresPermissions("waterManagement:WaterRegimeMonitor:export")
    @Log(title = "导出水情监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WaterRegimeMonitoringVO wt)
    {
        if (StringUtils.isEmpty(wt.getOrgCd())) {
            wt.setOrgCd(null);
        }
        if(wt.getOrgCd()!=null){
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wt.getOrgCd());
            wt.setOrgCd(addvAndOrgCodeInfo.get("resCode")+"");
        }

        List<WaterRegimeMonitoringVO> list = waterRegimeMonitoringService.selWaterRegimeMonitoringList(wt);
        ExcelUtil<WaterRegimeMonitoringVO> util = new ExcelUtil<WaterRegimeMonitoringVO>(WaterRegimeMonitoringVO.class);
        util.exportExcel(response, list, "水情监测数据");
    }

    /**
     * 导出测站实时图片列表
     */
    @RequiresPermissions("waterManagement:WaterRegimeMonitor:export")
    @Log(title = "导出水量监测数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export2")
    public void export2(HttpServletResponse response, WaterRegimeMonitoringVO wt)
    {
        if (StringUtils.isEmpty(wt.getOrgCd())) {
            wt.setOrgCd(null);
        }
        if(wt.getOrgCd()!=null){
            HashMap<String, Object> addvAndOrgCodeInfo = BusinessUtils.getAddvAndOrgCodeInfo(wt.getOrgCd());
            wt.setOrgCd(addvAndOrgCodeInfo.get("resCode")+"");
        }

        List<WaterRegimeMonitoringVO> list = waterRegimeMonitoringService.selWaterYieldRegimeMonitoringList(wt);
        ExcelUtil<WaterRegimeMonitoringVO> util = new ExcelUtil<WaterRegimeMonitoringVO>(WaterRegimeMonitoringVO.class);
        util.exportExcel(response, list, "水量监测数据");
    }
}
